﻿using Newtonsoft.Json;
using RestSharp;
namespace DongFangGuoKai.WMSPlatform.Commn.HttpModule
{
    /// <summary>
    /// Http请求帮助类
    /// </summary>
    public class CtmHttpClient : ICtmHttpClient
    {
        private readonly RestClient _client;


        public CtmHttpClient()
        {

        }

        public CtmHttpClient(RestClient client)
        {
            _client = client;


        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T">返回消息体</typeparam>
        /// <param name="resource">url</param>
        /// <param name="data">请求body</param>
        /// <param name="headers">请求头</param>
        /// <returns></returns>
        public T Post<T>(string resource, object data, Dictionary<string, string> headers = default, params Parameter[] parameters) where T : new()
        {
            var request = new RestRequest(resource, Method.Post);
            if (data != null)
            {
                request.AddJsonBody(data);

            }
            if (parameters != null && parameters.Length > 0)
            {
                foreach (var item in parameters)
                {
                    request.AddParameter(item);
                }

            }

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            return Execute<T>(request);
        }
        public T Post<T>(string resource, object data) where T : new()
        {
            return Post<T>(resource, data, default);
        }
        public T Post<T>(string resource, params Parameter[] parameters) where T : new()
        {
            return Post<T>(resource, null, null, parameters);
        }
        public T Post<T>(string resource) where T : new()
        {
            return Post<T>(resource, null, null);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">返回消息体</typeparam>
        /// <param name="resource">url</param>
        /// <param name="headers">请求头</param>
        /// <param name="parameter">请求参数</param>
        /// <returns></returns>
        public T Get<T>(string resource, Dictionary<string, string> headers = default, params Parameter[] parameter) where T : new()
        {

            var request = new RestRequest(resource, Method.Get)
             ;
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            if (parameter != null && parameter.Length > 0)
            {
                foreach (var item in parameter)
                {
                    request.AddParameter(item);
                }
            }


            return Execute<T>(request);

        }
        public T Get<T>(string resource, Parameter[] parameter) where T : new()
        {
            return Get<T>(resource, default, parameter);
        }
        public T Get<T>(string resource) where T : new()
        {
            return Get<T>(resource, default, default);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T">返回 T </typeparam>
        /// <param name="resource">url</param>
        /// <param name="headers">请求头</param>
        /// <param name="parameter">请求参数</param>
        /// <returns></returns>
        public T Put<T>(string resource, Parameter[] parameter, Dictionary<string, string> headers = default) where T : new()
        {
            var request = new RestRequest(resource, Method.Put);
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            if (parameter != null && parameter.Length > 0)
            {
                foreach (var item in parameter)
                {
                    request.AddParameter(item);
                }
            }

            return Execute<T>(request);
        }
        public T Put<T>(string resource, Parameter[] parameter) where T : new()
        {
            return Put<T>(resource, parameter, null);
        }
        public T Put<T>(string resource) where T : new()
        {
            return Put<T>(resource, null, null);
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="RestSharpException"></exception>
        private T Execute<T>(RestRequest request) where T : new()
        {

            var response = _client.Execute(request);
            if (response.IsSuccessful)
            {
                return JsonConvert.DeserializeObject<T>(response.Content);
            }
            if (!string.IsNullOrEmpty(response.Content))
            {
                try
                {
                    return JsonConvert.DeserializeObject<T>(response.Content);

                }
                catch
                {

                }
            }
            string errMsg = $"[请求Url：{request.Resource}] \n[请求参数：{JsonConvert.SerializeObject(response.Request.Parameters?.FirstOrDefault()?.Value)}] \n[详细信息：{response.Content ?? response.ErrorMessage}] \n [状态码：{response.StatusCode}]\n ";
            throw new RestSharpException(response.StatusCode, errMsg);
        }


    }
    public class RestSharpException : Exception
    {
        public RestSharpException(System.Net.HttpStatusCode statusCode, string errorMessage) : base(errorMessage)
        {
            StatusCode = statusCode;
        }

        public System.Net.HttpStatusCode StatusCode { get; }
    }

}
